package com.dog.basics.service.common.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.common.GuidePageAddDto;
import com.dog.basics.bean.dto.admin.common.GuidePageEditDto;
import com.dog.basics.bean.dto.admin.common.GuidePageFromDto;
import com.dog.basics.bean.model.common.DogGuidePage;
import com.dog.basics.bean.vo.admin.common.GuidePageVo;
import com.dog.basics.bean.vo.api.common.ApiGuidePageVo;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.common.util.R;
import com.dog.basics.dao.common.DogGuidePageMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.common.IDogGuidePageService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 引导页和启动页 服务实现类
 * </p>
 *
 * @author wph
 * @since 2020-05-27
 */
@Service
@AllArgsConstructor
public class DogGuidePageServiceImpl extends ServiceImpl<DogGuidePageMapper, DogGuidePage> implements IDogGuidePageService {

    /**
     * @Description: 分页引导页和启动页
     * @Param: status
     * @return: IPage<GuidePageVo>
     * @Author: wph
     * @Date:
     */
    @Override
    public IPage<GuidePageVo> getPageList(PageParam page, GuidePageFromDto obj) {
        IPage iPage = this.page(page, new LambdaQueryWrapper<DogGuidePage>()
                .like(StrUtil.isNotBlank(obj.getTitle()), DogGuidePage::getTitle, obj.getTitle())
                .like(StrUtil.isNotBlank(obj.getDescription()), DogGuidePage::getDescription, obj.getDescription())
                .eq(ObjectUtil.isNotNull(obj.getType()), DogGuidePage::getType, obj.getType())
                .orderBy(Boolean.TRUE, obj.getIsOrderByAvg(), DogGuidePage::getCreateTime));
        List<DogGuidePage> records = iPage.getRecords();
        iPage.setRecords(records.stream().map(e -> {
            GuidePageVo newsVo = new GuidePageVo();
            BeanUtil.copyProperties(e, newsVo);
            return newsVo;
        }).collect(Collectors.toList()));
        return iPage;
    }
    /**
     * @Description: 分页引导页和启动页
     * @Param: status
     * @return: IPage<GuidePageVo>
     * @Author: wph
     * @Date:
     */
    @Override
    public List<ApiGuidePageVo> getApiList(GuidePageFromDto obj) {
        List<DogGuidePage> list = this.list(Wrappers.<DogGuidePage>lambdaQuery().eq(ObjectUtil.isNotNull(obj.getType()),DogGuidePage::getType, obj.getType()).orderByDesc(DogGuidePage::getCreateTime));
        Validator.validateTrue(CollUtil.isNotEmpty(list), "引导页或启动页为空!");
        List<ApiGuidePageVo> newList = list.stream().map(e -> {
            ApiGuidePageVo guidePageVo = new ApiGuidePageVo();
            BeanUtil.copyProperties(e, guidePageVo);
            return guidePageVo;
        }).collect(Collectors.toList());
        return newList;
    }
    /**
     * @Description: 增加引导页和启动页
     * @Param: obj
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveObj(GuidePageAddDto obj) {
        DogGuidePage dogNews = new DogGuidePage();
        BeanUtil.copyProperties(obj, dogNews);
        dogNews.setCreateTime(DateUtil.currentSeconds());
        dogNews.setCreateBy(SecurityUtils.getSysUser().getUserId());
        return this.save(dogNews);
    }

    /**
     * @Description: 修改引导页和启动页
     * @Param: obj
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editObj(GuidePageEditDto obj) {
        DogGuidePage dogNews = this.getById(obj.getId());
        Validator.validateNotNull(dogNews, "无此数据");
        BeanUtil.copyProperties(obj, dogNews);
        dogNews.setUpdateTime(DateUtil.currentSeconds());
        dogNews.setUpdateBy(SecurityUtils.getSysUser().getUserId());
        return this.updateById(dogNews);
    }

    /**
     * @Description: 删除引导页和启动页
     * @Param: ids
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteObj(List<Long> id) {
        // 判断是否为空
        Validator.validateTrue(CollUtil.isNotEmpty(id), "主标识为空");
        return this.removeByIds(id);
    }
}
